#include <stdio.h>
#include <stdlib.h>
#include "utils.h"


// 农历数据结构（简化）
typedef struct {
    int year;
    int leap_month; // 闰月，0表示无
    int lunar_month_days[13]; // 12月+闰月，每月是大月(1)或小月(0)
} LunarYearInfo;

LunarYearInfo lunar_table[] = {
    {2020, 4, {1,0,1,0,1,1,0,1,0,1,0,1,0}},
    {2021, 0, {0,1,0,1,0,1,0,1,1,0,1,0,0}},
    {2022, 0, {1,0,1,0,1,0,1,1,0,1,0,1,0}},
};

int solar_month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};



int GetSolarMonthDays(int year, int month) {
    if (month == 2) return IsLeapYear(year) ? 29 : 28;
    return solar_month_days[month-1];
}

int ConvertSolarToLunar(int year, int month, int day, int *out_lunar_month, int *out_leap) {
    LunarYearInfo *info = NULL;
    for (int i=0; i < sizeof(lunar_table)/sizeof(lunar_table[0]); i++) {
        if (lunar_table[i].year == year) {
            info = &lunar_table[i];
            break;
        }
    }
    if (!info) return -1;

    int days_offset = 0;
    for (int m=1; m<month; m++) days_offset += GetSolarMonthDays(year, m);
    days_offset += day;

    int lunar_new_year_day = 25; // 2020示例
    int offset = days_offset - lunar_new_year_day + 1;

    int lunar_month = 1;
    int lunar_day = 0;
    int leap = 0;
    int i = 0;
    while (offset > 0 && info->lunar_month_days[i]) {
        int month_len = info->lunar_month_days[i] ? 30 : 29;
        if (offset <= month_len) {
            lunar_day = offset;
            leap = (info->leap_month > 0 && lunar_month == info->leap_month) ? 1 : 0;
            break;
        }
        offset -= month_len;
        lunar_month++;
        i++;
    }
    *out_lunar_month = lunar_month;
    *out_leap = leap;
    return lunar_day;
}

const char* tiangan[] = {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};
const char* dizhi[] = {"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};

void CalcYearGanZhi(int year, char *out) {
    int gan = (year - 4) % 10;
    int zhi = (year - 4) % 12;
    sprintf(out, "%s%s年", tiangan[gan], dizhi[zhi]);
}

typedef struct {
    int month;
    int day;
    const char* name;
} SolarTerm;

SolarTerm solarTerms[24] = {
    {1, 6, "小寒"}, {1, 20, "大寒"},
    {2, 4, "立春"}, {2, 19, "雨水"},
    {3, 6, "惊蛰"}, {3, 21, "春分"},
    {4, 5, "清明"}, {4, 20, "谷雨"},
    {5, 6, "立夏"}, {5, 21, "小满"},
    {6, 6, "芒种"}, {6, 21, "夏至"},
    {7, 7, "小暑"}, {7, 22, "大暑"},
    {8, 7, "立秋"}, {8, 23, "处暑"},
    {9, 7, "白露"}, {9, 23, "秋分"},
    {10, 8, "寒露"}, {10, 23, "霜降"},
    {11, 7, "立冬"}, {11, 22, "小雪"},
    {12, 7, "大雪"}, {12, 22, "冬至"}
};

const char* CheckSolarTerm(int month, int day) {
    for (int i = 0; i < 24; i++) {
        if (solarTerms[i].month == month && solarTerms[i].day == day) {
            return solarTerms[i].name;
        }
    }
    return NULL;
}

int DaysToNextSolarTerm(int month, int day) {
    for (int i = 0; i < 24; i++) {
        if (solarTerms[i].month > month || (solarTerms[i].month == month && solarTerms[i].day > day)) {
            int diff = (solarTerms[i].month - month) * 30 + (solarTerms[i].day - day);
            return diff;
        }
    }
    return (12 - month) * 30 + (31 - day) + solarTerms[0].day;
}


void PrintMonthCalendar(int year, int month) {
    int days = GetSolarMonthDays(year, month);
    int firstWeekday = GetWeekday(year, month, 1);

    printf("\n======== %d年 %02d月 月历 ========\n", year, month);
    printf("日 一 二 三 四 五 六\n");

    for (int i = 0; i < firstWeekday; i++) printf("   ");

    for (int day = 1; day <= days; day++) {
        int lunar_month, lunar_leap;
        int lunar_day = ConvertSolarToLunar(year, month, day, &lunar_month, &lunar_leap);
        const char *solarTerm = CheckSolarTerm(month, day);

        char lunar_str[10];
        if (lunar_day > 0) {
            if (lunar_leap) {
                sprintf(lunar_str, "闰%d/%d", lunar_month, lunar_day);
            } else {
                sprintf(lunar_str, "%d/%d", lunar_month, lunar_day);
            }
        } else {
            sprintf(lunar_str, "--");
        }

        char term_mark = (solarTerm != NULL) ? '*' : ' ';
        printf("%2d%c", day, term_mark);
        if ((firstWeekday + day - 1) % 7 == 6) printf("\n");
        else printf(" ");
    }
    printf("\n==============================\n");
}

// === 子模块函数 ===
void querylunar_1(int year,int month,int day) {

    PrintMonthCalendar(year, month);

    char ganzhi[20];
    CalcYearGanZhi(year, ganzhi);
    printf("当前年份的天干地支为：%s\n", ganzhi);

    const char *term = CheckSolarTerm(month, day);
    if (term) {
        printf("今天是节气：%s\n", term);
    } else {
        int days_left = DaysToNextSolarTerm(month, day);
        printf("今天不是节气，距离下一个节气还有 %d 天\n", days_left);
    }
}
